<template>
  <el-dialog :visible.sync="dialogVisible" title="子系统信息" @close="resetForm">
    <el-form ref="formData" :model="formData" label-width="100px" :rules="roles">
      <el-row>
        <el-col :span="11">
          <el-form-item label="子系统名" class="is-required" prop="name">
            <el-input ref="name" v-model="formData.name" name="name" />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="子系统代码" class="is-required" prop="code">
            <el-input ref="code" v-model="formData.code" name="code" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="子系统链接" class="is-required" prop="link">
            <el-input ref="link" v-model="formData.link" name="link" />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="子系统备注" prop="remark">
            <el-input ref="remark" v-model="formData.remark" name="remark" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="子系统图标" class="is-required" prop="imageUrl">
            <el-select v-model="formData.imageUrl">
              <el-option
                v-for="item in iconOptions"
                :key="item.value"
                :value="item.value"
              >
                <span class="custom-option">
                  <img :src="item.imageUrl" alt="" class="option-image">
                </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="editData">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      // 对话框可见状态
      dialogVisible: false,
      // 表单数据
      formData: {
        id: '',
        name: '',
        code: '',
        remark: '',
        link: '',
        imageUrl: ''
      },
      // 表单验证
      roles: {
        name: [{ required: true, message: '子系统名不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '子系统代码不能为空', trigger: 'blur' }],
        link: [{ required: true, message: '子系统链接不能为空', trigger: 'blur' }],
        imageUrl: [{ required: true, message: '子系统图标不能为空', trigger: 'blur' }]
      },
      iconOptions: [
        { value: 'https://s2.loli.net/2022/08/12/Ny6HGA3mESarbVe.png', imageUrl: 'https://s2.loli.net/2022/08/12/Ny6HGA3mESarbVe.png' },
        { value: 'https://s2.loli.net/2022/08/12/h2fgmF3pzQK6WCo.png', imageUrl: 'https://s2.loli.net/2022/08/12/h2fgmF3pzQK6WCo.png' },
        { value: 'https://s2.loli.net/2022/08/12/gvoz8XfS4nKC1UN.png', imageUrl: 'https://s2.loli.net/2022/08/12/gvoz8XfS4nKC1UN.png' },
        { value: 'https://s2.loli.net/2022/08/12/CL9n5rlhJducyVt.png', imageUrl: 'https://s2.loli.net/2022/08/12/CL9n5rlhJducyVt.png' },
        { value: 'https://s2.loli.net/2022/08/12/SMh1gkazWOYPxnC.png', imageUrl: 'https://s2.loli.net/2022/08/12/SMh1gkazWOYPxnC.png' },
        { value: 'https://s2.loli.net/2022/08/12/HxGT5i6nNtkUbZR.png', imageUrl: 'https://s2.loli.net/2022/08/12/HxGT5i6nNtkUbZR.png' }
      ]
    }
  },
  methods: {
    resetForm() {
      this.formData = {
        id: '',
        name: '',
        code: '',
        remark: '',
        link: '',
        icon: ''
      }
    },
    editData() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.$emit('edit', this.formData) // 发送事件给父组件
        } else {
          this.$message.error('请输入完整信息')
        }
      })
    }
  }
}
</script>

<style scoped>
.custom-option {
  display: flex;
  align-items: center; /* 垂直居中 */
}

.option-image {
  width: 20px; /* 根据需要设置图片宽度 */
  height: 20px; /* 根据需要设置图片高度 */
}
</style>
